package com.service.user.impl;

import com.dao.WalletDao;
import com.entity.Notice;
import com.entity.WalletRecord;
import com.service.user.NoticeService;
import com.service.user.WalletService;
import com.utils.FileUploadUtil;
import com.utils.SqlSessionGetter;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

/**  */
@Service
public class WalletServiceImpl implements WalletService {

  @Autowired NoticeService noticeService;

  @Override
  public String chPaymentCodeUrl(String userId, MultipartFile file) {
    // 保存新的图片
    String newImageURL =
        FileUploadUtil.uploadFile(
            file,
            UUID.randomUUID().toString().replace("-", ""),
            FileUploadUtil.QR_CODE_SUBDIR,
            FileUploadUtil.IMAGE_SUFFIX);
    if (newImageURL == null) {
      // 文件格式错误
      return null;
    }

    SqlSession session = SqlSessionGetter.getSqlSession();
    WalletDao dao = session.getMapper(WalletDao.class);
    // 取得原来图片的地址
    String oldImageURL = dao.selectQrCode(userId);
    // 将原来的图片删除
    FileUploadUtil.delFile(oldImageURL);

    // 保存新的url
    dao.updateQrcode(userId, newImageURL);
    session.commit();
    session.close();
    return newImageURL;
  }

  @Override
  public int requestWithdrawal(String userId, float count) {
    // 更新用户的账户余额
    SqlSession session = SqlSessionGetter.getSqlSession();
    WalletDao dao = session.getMapper(WalletDao.class);
    int ret = 0;
    try {
      ret = dao.updateCoinCount(userId, count, count);
      // 添加一条提现记录
      dao.insertWithDrawRecord(userId, count);
    } catch (Exception e) {
      System.err.println("提现请求sql执行错误");
    }
    session.commit();
    session.close();

    // 添加提现请求通知
    noticeService.addNotice(userId, Notice.WITHDRAW_COMMIT, null, 1, count);
    return ret;
  }

  @Override
  public List<WalletRecord> getWalletRecord(
      String userId, Integer type, Integer pageSize, Integer pageIndex) {
    List<WalletRecord> record = null;
    SqlSession session = SqlSessionGetter.getSqlSession();
    WalletDao dao = session.getMapper(WalletDao.class);
    try {
      Integer start = (pageSize == null ? null : pageSize * pageIndex);
      record = dao.selectWalletRecord(userId, type, start, pageSize);
    } catch (Exception e) {
      System.err.println("获取流水记录sql错误");
      e.printStackTrace();
    }
    session.close();
    return record;
  }

  @Override
  public int buyCoinSuccess(String userId, float count) {
    int ret = changeRest(userId, count, WalletRecord.COIN_BUY);
    noticeService.addNotice(userId, Notice.BUY_COIN_SUCCESS, null, 1, count);
    return ret;
  }

  @Override
  public int pay(String userId, float count) {
    return changeRest(userId, -count, WalletRecord.SPEND);
  }

  @Override
  public int income(String userId, float count) {
    return changeRest(userId, count, WalletRecord.INCOME);
  }

  /**
   * 更新钱包余额,并添加记录
   *
   * @param userId 用户编号
   * @param addCount 增加的数目
   * @return 1/0
   */
  private int changeRest(String userId, float addCount, int type) {

    SqlSession session = SqlSessionGetter.getSqlSession();
    WalletDao dao = session.getMapper(WalletDao.class);
    int row = 0;
    try {
      // 更新钱包数目
      row = dao.updateCoinCount(userId, -addCount, 0);
      // 添加流水记录
      dao.insertWalletRecord(userId, addCount, type);
    } catch (Exception e) {
      System.err.println("钱包余额更新失败");
      e.printStackTrace();
    }
    session.commit();
    session.close();
    return row;
  }
}
